package com.yuanduan.mplus.model;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.yuanduan.mplus.model.entity.User;

/**
 * Created by cj on 2016/5/28.
 * Email:781806481@qq.com
 */
public class DBManager {
    private static DBManager manager;
    private SQLiteDatabase db;

    private static final String DB_NAME="mplus.db";
    private static final int DB_VERSION=1;

    private DBManager(Context context){
        DBOpenHelper openHelper=new DBOpenHelper(context,DB_NAME,null,DB_VERSION);
        db=openHelper.getWritableDatabase();
    }

    public synchronized static DBManager getInstance(Context context){
        if(manager==null){
            manager=new DBManager(context);
        }
        return manager;
    }

    /**
     * 从数据库中获取User
     * @return 若数据库中没有，则返回一个字段都是默认的User，若有，返回包含数据库内容的User
     */
    public User getUser(){
        User user=new User();
        Cursor cursor=db.query("User",null,null,null,null,null,null);
        if(cursor.moveToFirst()){
            do{
                user.set_id(cursor.getInt(cursor.getColumnIndex("_id")));
                user.setId(cursor.getInt(cursor.getColumnIndex("id")));
                user.setUserPhone(cursor.getString(cursor.getColumnIndex("userPhone")));
                user.setUserPwd(cursor.getString(cursor.getColumnIndex("userPwd")));
                user.setRealName(cursor.getString(cursor.getColumnIndex("realName")));
                user.setUserHead(cursor.getString(cursor.getColumnIndex("userHead")));
                user.setUserType(cursor.getString(cursor.getColumnIndex("userType")));
                user.setCarTeamID(cursor.getString(cursor.getColumnIndex("carTeamId")));
                user.setLookCarID(cursor.getString(cursor.getColumnIndex("lockCarId")));
                user.setIDCard(cursor.getString(cursor.getColumnIndex("idcard")));
                user.setAge(cursor.getString(cursor.getColumnIndex("age")));
                user.setAttributionID(cursor.getString(cursor.getColumnIndex("attributionId")));
                user.setProfessional(cursor.getString(cursor.getColumnIndex("professional")));
                user.setCreateTimer(cursor.getString(cursor.getColumnIndex("createTime")));
                user.setCarNumber(cursor.getString(cursor.getColumnIndex("carNumber")));
                user.setCarTeamName(cursor.getString(cursor.getColumnIndex("carTeamName")));
                user.setSex(cursor.getInt(cursor.getColumnIndex("sex")));
                user.setAudit(cursor.getInt(cursor.getColumnIndex("audit")));
                user.setReason(cursor.getString(cursor.getColumnIndex("reason")));
            }while(cursor.moveToNext());
        }
        return user;
    }


    /**
     * 将User保存到数据库中
     * @param user
     */
    public void setUser(User user){
        deleteUser();
        ContentValues values=new ContentValues();
//        values.put("_id",user.get_id());
        values.put("id",user.getId());
        values.put("userPhone",user.getUserPhone());
        values.put("userPwd",user.getUserPwd());
        values.put("realName",user.getRealName());
        values.put("userHead",user.getUserHead());
        values.put("userType",user.getUserType());
        values.put("carTeamId",user.getCarTeamID());
        values.put("lockCarId",user.getLookCarID());
        values.put("age",user.getAge());
        values.put("idcard",user.getIDCard());
        values.put("professional",user.getProfessional());
        values.put("attributionId",user.getAttributionID());
        values.put("createTime",user.getCreateTimer());
        values.put("carNumber",user.getCarNumber());
        values.put("carTeamName",user.getCarTeamName());
        values.put("sex",user.getSex());
        values.put("audit",user.getAudit());
        values.put("reason",user.getReason());
        db.insert("User",null,values);
    }

    /**
     * 删除数据库中的User
     */
    public void deleteUser(){
        db.delete("User",null,null);
    }

    /**
     * 更新User
     * @param user
     */
    public void updateUser(User user){
        ContentValues values=new ContentValues();
        values.put("id",user.getId());
        values.put("userPhone",user.getUserPhone());
        values.put("userPwd",user.getUserPwd());
        values.put("realName",user.getRealName());
        values.put("userHead",user.getUserHead());
        values.put("userType",user.getUserType());
        values.put("carTeamId",user.getCarTeamID());
        values.put("lockCarId",user.getLookCarID());
        values.put("idcard",user.getIDCard());
        values.put("age",user.getAge());
        values.put("attributionId",user.getAttributionID());
        values.put("professional",user.getProfessional());
        values.put("createTime",user.getCreateTimer());
        values.put("carNumber",user.getCarNumber());
        values.put("carTeamName",user.getCarTeamName());
        values.put("sex",user.getSex());
        values.put("audit",user.getAudit());
        values.put("reason",user.getReason());
        db.update("User",values,null,null);
    }
}
